package org.smr.ministore.controller;

import javax.annotation.Resource;

import org.blade.msg.DataMessage;
import org.blade.msg.ErrorCode;
import org.blade.msg.Message;
import org.blade.utils.Pager;
import org.smr.ministore.entities.BrandOrgan;
import org.smr.ministore.entities.GoodsTypeOrgan;
import org.smr.ministore.entities.vo.BrandOrganQuery;
import org.smr.ministore.entities.vo.GoodsTypeOrganQuery;
import org.smr.ministore.service.impl.BrandOrganServiceFacade;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;
import java.util.Objects;


@Controller("BrandOrganController")
public class BrandOrganController {

	@Resource
	private BrandOrganServiceFacade service;
	
	/**
	 * 保存编辑后的BrandOrgan信息
	 * @param brandOrgan
	 * @return
	 */
	@ResponseBody
	@RequestMapping(method = RequestMethod.PATCH, value = "/BrandOrgan/edit")
	public Message modify(@RequestBody BrandOrgan brandOrgan){
		Message msg = new Message();

		if(Objects.isNull(brandOrgan.getOrganId())) {
		    msg.setStatus(ErrorCode.Business.Invalid_organId);
		    return msg;
        }

		if(null == brandOrgan.getId()){
			msg.setStatus(service.save(brandOrgan));
		}else{
			msg.setStatus(service.updateById(brandOrgan));
		}
		return msg;
	}
	
	/**
	* 跳转到列表查询BrandOrgan
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/BrandOrgan/list-page")
	public String toList(){
		return "/BrandOrgan/BrandOrgan-list";
	}

	/**
	*  根据id查询BrandOrgan
	* @return
	*/
	@ResponseBody
	@RequestMapping(method = RequestMethod.GET, value = "/BrandOrgan")
	public BrandOrgan get( Long id ){
		return service.queryById(id);
	}


    /**
	 * 分页查询BrandOrgan信息
	 * @param BrandOrgan
	 * @param pageSize
	 * @param pageNum
	 * @return
	 */
	@ResponseBody
	@RequestMapping( value = "/BrandOrgan/list")
	public Pager<BrandOrgan> list(@RequestBody BrandOrgan BrandOrgan , long pageSize, long pageNum ){

		return service.paginate(BrandOrgan, pageSize, pageNum);
	}
	
	
	/**
	 * 根据ID删除BrandOrgan信息
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping( value = "/BrandOrgan/remove",method = RequestMethod.DELETE)
	public Message remove( Long id){
		
		Message msg = new Message();
		msg.setStatus(service.deleteById(id));
		return msg;
	}

	@ResponseBody
	@RequestMapping( value = "/BrandOrgan/queryAlreadyAssignedOrgans",method = RequestMethod.POST )
	public DataMessage<List<BrandOrgan>> queryAlreadyAssignedOrgans(@RequestBody BrandOrganQuery goodsTypeOrganQuery){

		List<BrandOrgan> brandOrganList = this.service.queryAlreadyAssignedOrgans(goodsTypeOrganQuery);
		return DataMessage.success(brandOrganList);
	}


}
